package makaveli.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView lowerText;
	TextView upperText;
	static StringBuffer strBuff = new StringBuffer("0");
	//DoubleEvaluator result = new DoubleEvaluator();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lowerText = (TextView) findViewById(R.id.lowerText);
		upperText = (TextView) findViewById(R.id.upperText);
		upperText.setText(strBuff);
		
		for (int i = 0; i < 15; i++)
		{
			((Button)findViewById(getResources().getIdentifier("button" + i, "id", getPackageName()))).
															setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (upperText.getText().equals("0"))
						upperText.setText(((Button)v).getText());
					else
						upperText.append(((Button)v).getText());
				}
			});
		}
		
		((Button)findViewById(R.id.buttonEqual)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				lowerText.setText(calcResult(upperText.getText().toString()));
				upperText.setText("0");
			}
		});
	}
	
	@Override
	protected  void onDestroy()
	{
		super.onDestroy();
		strBuff.setLength(0);
		strBuff.append(upperText.getText());
	}
	
	private String calcResult(String strBuffText)
	{
		//http://javaluator.sourceforge.net/en/download
		return "result";//result.evaluate(strBuffText).toString();
	}
}